#include "lcd12864.h"


void LCD12864_Delay1ms(uint c)
{
    uchar a,b;
	for(; c>0; c--)
	{
	    for(b=199; b>0; b--)
		{
	        for(a=1; a>0; a--);
		}
	}
}

/*******************************************************************************
* ? ? ?         : LCD12864_Busy
* ????		   : ??LCD???
* ?    ?         : ?
* ?    ?         : 1?0(1????,0???)
*******************************************************************************/

//uchar LCD12864_Busy(void)
//{
//	uchar i = 0;

//	LCD12864_RS = 0;   	//????
//	LCD12864_RW = 1;	//????

//	LCD12864_EN = 1;
//	LCD12864_Delay1ms(1);
//	
//	LCD12864_Delay1ms(10);

//	LCD12864_EN = 0;

//	return 1;
//}

/*******************************************************************************
* ? ? ?         : LCD12864_WriteCmd
* ????		   : ???
* ?    ?         : cmd
* ?    ?         : ?
*******************************************************************************/

void LCD12864_WriteCmd(uchar cmd)
{
	uchar i;
	i = 0;
	
	LCD12864_Delay1ms(20);
	
	LCD12864_RS = 0;     //????
	LCD12864_RW = 0;     //????
	LCD12864_EN = 0;     //??????

	LCD12864_DATAPORT = cmd;   //????

	LCD12864_EN = 1;		   //???
	LCD12864_Delay1ms(5);
	LCD12864_EN = 0;    					
}

/*******************************************************************************
* ? ? ?         : LCD12864_WriteData
* ????		   : ???
* ?    ?         : dat
* ?    ?         : ?
*******************************************************************************/

void LCD12864_WriteData(uchar dat)
{
	uchar i;
	i = 0;

	LCD12864_Delay1ms(20);

	LCD12864_RS = 1;     //????
	LCD12864_RW = 0;     //????
	LCD12864_EN = 0;     //??????

	LCD12864_DATAPORT = dat;   //????

	LCD12864_EN = 1;		   //???
	LCD12864_Delay1ms(5);
	LCD12864_EN = 0;    								
}

/*******************************************************************************
* ? ? ?         : LCD12864_ReadData
* ????		   : ????
* ?    ?         : ?
* ?    ?         : ????8???
*******************************************************************************/
#ifdef LCD12864_PICTURE

uchar LCD12864_ReadData(void)
{
	uchar i, readValue;

	i = 0;
	
	LCD12864_Delay1ms(20);

	LCD12864_RS = 1;       //????
	LCD12864_RW = 1;
	LCD12864_EN = 0;
	LCD12864_Delay1ms(1);  //??

	LCD12864_EN = 1;
	LCD12864_Delay1ms(1);
	readValue = LCD12864_DATAPORT;
	LCD12864_EN = 0;

	return readValue;
}

#endif

/*******************************************************************************
* ? ? ?         : LCD12864_Init
* ????		   : ???LCD12864
* ?    ?         : ?
* ?    ?         : ?
*******************************************************************************/

void LCD12864_Init()
{
	LCD12864_WriteCmd(0x38);  //????????
	LCD12864_WriteCmd(0x0c);  //???,???
	LCD12864_WriteCmd(0x06);
   LCD12864_WriteCmd(0x01);     //??LCD12864?????
}

/*******************************************************************************
* ? ? ?         : LCD12864_ClearScreen
* ????		   : ??????,LCD12864?01H??????,?????????
*                  * ???
* ?    ?         : ?
* ?    ?         : ?
*******************************************************************************/
#ifdef LCD12864_PICTURE

void LCD12864_ClearScreen(void)
{
	uchar i,j;

	LCD12864_WriteCmd(0x34);	  //???????

	for(i=0;i<32;i++)			  //??LCD????32????????
	{
		LCD12864_WriteCmd(0x80+i);		  //??????Y??
		LCD12864_WriteCmd(0x80);		  //??????X??
		for(j=0;j<32;j++)		  //????16?,????????????,?
		{						  //???32?????????????????
			LCD12864_WriteData(0xFF);	  //??1,?????????????
		}
	}

	LCD12864_WriteCmd(0x36);	 //0x36????????????
	LCD12864_WriteCmd(0x30);	 //???????
}

#endif
/*******************************************************************************
* ? ? ?         : LCD12864_SetWindow
* ????		   : ???????????????????:x????,y????
* ?    ?         : x, y
* ?    ?         : ?
*******************************************************************************/

void LCD12864_SetWindow(uchar x, uchar y)
{
	uchar pos;

	if(x == 0)	   // ???????80H
	{
		x = 0x80;
	}
		
	else if(x == 1)  //???????90H
	{
		x = 0x90;	
	}
	
	else if(x == 2)  //???????88H
	{
		x = 0x88;
	}
	
	else if(x == 3)
	{
		x = 0x98;
	}
	
	pos = x + y;
	LCD12864_WriteCmd(pos);
}

